---
title: "The last barrier against the fragmentation of public sphere is still there: longitudinal evidence of a common agenda between news media and SNSs on the Brazilian climate change debate"
subtitle: "Research report"
date: "21/07/2022"
output:
  html_document:
    toc: true # table of contents
    error: FALSE
    warning: FALSE
    message: FALSE
    toc_float: true # flutuar a esquerda
    toc_collapsed: true # colapsado
    smooth_scroll: true # scroll abre 
    fig_width: 12 # comprimento da figura
    fig_height: 8 # altura da figura
    theme:
      bg: "#fafafa"
      fg: "#121212"
      primary: "#5cb0bf"
      secondary: "#5cb0bf"
      base_font:
        google: Roboto Condensed
      heading_font:
        google: Roboto Condensed

---


```{r setup, include=FALSE}
knitr::opts_chunk$set(message = FALSE, error = F, echo = F)

# library -----------------------------------------------------------------
# Função para verificar se um pacote está instalado; se não, instalá-lo e depois carregá-lo
verificar_e_carregar <- function(pacote) {
  if (!require(pacote, character.only = TRUE)) {
    install.packages(pacote)
    library(pacote, character.only = TRUE)
  }
}

# Lista de pacotes a serem carregados
pacotes <- c("tidyverse", "janitor", "formattable", "kableExtra", "scales", 
             "hrbrthemes", "htmltools", "reactable", "ggtext", 
             "glue", "tidyquant", "qdapRegex", "ggrepel", "corrplot", "sjPlot", 
             "GGally", "stringdist", "here")

# Aplicar a função `verificar_e_carregar` a cada pacote
sapply(pacotes, verificar_e_carregar)




# p value
# mat : is a matrix of data
# ... : further arguments to pass to the native R cor.test function
cor.mtest <- function(mat, ...) {
    mat <- as.matrix(mat)
    n <- ncol(mat)
    p.mat<- matrix(NA, n, n)
    diag(p.mat) <- 0
    for (i in 1:(n - 1)) {
        for (j in (i + 1):n) {
            tmp <- cor.test(mat[, i], mat[, j], ...)
            p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
        }
    }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
  p.mat
}


gpairs_lower <- function(g){
  g$plots <- g$plots[-(1:g$nrow)]
  g$yAxisLabels <- g$yAxisLabels[-1]
  g$nrow <- g$nrow -1
  
  g$plots <- g$plots[-(seq(g$ncol, length(g$plots), by = g$ncol))]
  g$xAxisLabels <- g$xAxisLabels[-g$ncol]
  g$ncol <- g$ncol - 1
  
  g
}

options(digits=2)
options(scipen = 999)


# Data --------------------------------------------------------------------
load(here("mudancas_climaticas_data_br_full.Rdata"))

```


```{r}

cor = "skyblue"

guias <- theme(
  plot.title.position = "plot",
  plot.caption = element_textbox_simple(hjust = 0, halign = T),
  plot.subtitle = element_textbox_simple(
    lineheight = 0,
    padding = margin(5.5, 5.5, 5.5, 5.5),
    margin = margin(0, 0, 5.5, 0)
  ))

# Função para adicionar unidades em rótulo de gráfico
addUnits <- function(n) {
  labels <- ifelse(n < 1000, n,  # less than thousands
                   ifelse(n < 1e6, paste0(round(n / 1e3), 'k'),  # in thousands
                          ifelse(
                            n < 1e9, paste0(round(n / 1e6), 'M'),  # in millions
                            ifelse(
                              n < 1e12,
                              paste0(round(n / 1e9), 'B'),
                              # in billions
                              ifelse(n < 1e15, paste0(round(n /
                                                              1e12), 'T'), # in trillions
                                     'too big!')
                            )
                          )))
  return(labels)
}

tema =  hrbrthemes::theme_ipsum(axis_title_family = "Cambria",
                                #axis_title_face = "bold",
                                axis_text_size = 11, 
                                caption_size = 11,axis_title_just = "center", 
                                axis_title_size =   11,
                                plot_title_size = 11, 
                                subtitle_size = 11)  



```



```{r agregar, include=FALSE}
# names
fb <- fb %>%  
  filter(dia >= as.Date('2014-01-01'))
insta <- insta  %>% 
  filter(dia >= as.Date('2014-01-01'))
twitter <- twitter  %>% 
  filter(dia >= as.Date('2014-01-01'))

# total interactions
twitter <- twitter %>% mutate(total_interactions = public_metrics.retweet_count + public_metrics.reply_count +  public_metrics.like_count  + public_metrics.quote_count)

# agregar

agregado <- insta %>% 
  select(post_id, total_interactions, dia, mes_dia, platform) %>% 
  bind_rows(select(fb, post_id, total_interactions, dia, mes_dia, platform)) %>%   bind_rows(select(twitter, post_id, total_interactions, dia, mes_dia, platform))

# press
press_br = read_rds("press_ge.rds") %>% 
  filter(mes_dia >= as.Date("2014-01-01"), mes_dia <= as.Date("2022-12-01"))

press_agregado <- press_br %>% 
  group_by(mes_dia) %>% 
  summarise(n = sum(n)) %>% 
  add_column(platform = "Press")

```


This research report studies **`r prettyNum(n_distinct(agregado$post_id), big.mark = ".")`** publications, considering **`r prettyNum(n_distinct(fb$url), big.mark = ".")`** on Facebook with **`r prettyNum(sum(fb$total_interactions), big.mark = ".")`** interactions; **`r prettyNum(n_distinct(twitter$post_id), big.mark = ".")`** on Twitter with **`r prettyNum(sum(twitter$total_interactions), big.mark = ".")`** interactions; and **`r prettyNum(n_distinct(insta$url), big.mark = ".")`** posts on Instagram with **`r prettyNum(sum(insta$total_interactions), big.mark = ".")`** interactions



# Main metrics {.tabset .tabset-pills}


## Multiplaform

```{r geral_kpi, align = "center", message = F, error=F, echo = F, warning = F}

ag <-  agregado %>%
  summarise(Posts = n(), 
             mean_interactions = mean(total_interactions, na.rm = T),
             total_interactions = sum(total_interactions, na.rm = T)) %>% 
  gather(key = "Categoria", value = "Valores")



kpi <- reactable(
  ag,
  highlight = T,
  fullWidth = F,
  defaultColDef = colDef(align = "center", minWidth = 200),
  columns = list(
    Valores = colDef(name = "Valores", 
                          cell = function(value){value <- format(value, big.mark = ".")}
    )
  ),
  style = list(fontFamily = "Roboto Condensed")
)


div(kpi, align = "center")
```


## Facebook

```{r geral_fb, align = "center", message = F, error=F, echo = F, warning = F}

ag_fb <- fb %>%
  summarise(
    Posts = n_distinct(url, na.rm = T),
    Likes = as.integer(sum(likes, na.rm = T)),
    Likes_mean = as.integer(mean(likes, na.rm = T)),
    Views = as.integer(sum(total_views, na.rm = T)),
    Comments = as.integer(sum(comments, na.rm = T)),
    Comments_mean = as.integer(mean(comments, na.rm = T)),
    Shares = as.integer(sum(shares, na.rm = T))
  ) %>%
  pivot_longer(cols = Posts:Shares, names_to = "Métrica") %>%
  mutate(Métrica = prettyNum(Métrica, big.mark = ".", decimal.mark = ",")) %>%
  rename(Valor = value)


rct1 <- reactable(
  ag_fb,
  highlight = T,
  fullWidth = F,
  columns = list(
    Valor = colDef(name = "Valores", 
                     cell = function(value){value <- format(value, big.mark = ".")}
    )
  ),
  defaultColDef = colDef(align = "center", minWidth = 200),
  style = list(fontFamily = "Roboto Condensed")
)


div(rct1, align = "center")
```



## Instagram

```{r geral_insta, align = "center", message = F, error=F, echo = F, warning = F}


ag_insta <- insta %>%
  summarise(
    Posts = n_distinct(url, na.rm = T),
    Likes = as.integer(sum(likes, na.rm = T)),
    Likes_mean = as.integer(mean(likes, na.rm = T)),
    Views = as.integer(sum(total_views, na.rm = T)),
    Comments = as.integer(sum(comments, na.rm = T)),
    Comments_mean = as.integer(mean(comments, na.rm = T))) %>%
  pivot_longer(cols = Posts:Comments_mean, names_to = "Métrica") %>%
  mutate(Métrica = prettyNum(Métrica, big.mark = ".", decimal.mark = ",")) %>%
  rename(Valor = value)


rct2 <- reactable(
  ag_insta,
  highlight = T,
  fullWidth = F,
  columns = list(
    Valor = colDef(name = "Valores", 
                   cell = function(value){value <- format(value, big.mark = ".")}
    )
  ),
  defaultColDef = colDef(align = "center", minWidth = 200),
  style = list(fontFamily = "Roboto Condensed")
)


div(rct2, align = "center")
```



## Twitter

```{r geral_itw, align = "center", message = F, error=F, echo = F, warning = F}


ag_tw <- twitter %>%
  summarise(
    Posts = n_distinct(post_id, na.rm = T),
   Likes = as.integer(sum(public_metrics.like_count, na.rm = T)),
    Likes_mean = as.integer(mean(public_metrics.like_count, na.rm = T)),
   Comments = as.integer(sum(public_metrics.reply_count, na.rm = T)),
    Comments_mean = as.integer(mean(public_metrics.reply_count, na.rm = T)),
    RTs_mean = as.integer(mean(public_metrics.retweet_count, na.rm = T)),
    RTs = as.integer(sum(public_metrics.retweet_count, na.rm = T))
   ) %>%
  pivot_longer(cols = Posts:RTs, names_to = "Métrica") %>%
  mutate(Métrica = prettyNum(Métrica, big.mark = ".", decimal.mark = ",")) %>%
  rename(Valor = value)


rct2 <- reactable(
  ag_tw,
  highlight = T,
  fullWidth = F,
  columns = list(
    Valor = colDef(name = "Valores", 
                   cell = function(value){value <- format(value, big.mark = ".")}
    )
  ),
  defaultColDef = colDef(align = "center", minWidth = 200),
  style = list(fontFamily = "Roboto Condensed")
)


div(rct2, align = "center")
```


# Time series {.tabset .tabset-pills}

## Monthtly - by platform

```{r ag_mensal, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=8}


t <- agregado  %>% 
  count(mes_dia) 


t %>% 
  ggplot(aes(x = mes_dia, y = n)) + 
  geom_line(aes(group = 1), color= cor, size = 3)+
  geom_point(color=cor, fill = "white", size = 3.5, shape = 21)+
  geom_text_repel(data = t %>%  top_n(3, n), aes(label= prettyNum(n, 
                                 big.mark = ".")), 
            hjust=-.6, vjust=.4, size = 7, 
            family = "Roboto Condensed",
            color = cor)  + # keep expand
  labs(x="", y="Posts\n",
        caption="<br>**Source**: Twitter Instagram e Facebook<br>") + 
 theme(legend.position = "none") +
  scale_x_date(limits = as.Date(c('2014/1/1', '2022/12/1')),
                       date_breaks   = "2 months", 
               labels=date_format("%m\n%y"), 
               expand = c(0,0))+ tema +
  theme(
    plot.title.position = "plot",
    plot.caption = element_textbox_simple(hjust = 0, halign = T),
    plot.subtitle = element_textbox_simple(
      lineheight = 1,
      padding = margin(5.5, 5.5, 5.5, 5.5),
      margin = margin(0, 0, 5.5, 0)
    )) +
  scale_y_continuous(labels = addUnits)



```


## Monthly - press

```{r ag_mensal_imprensa, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=8}


press_br %>% 
  ggplot(aes(x = mes_dia, y = n, group = press, color= press)) + 
  geom_line(size = 3, alpha  = .7)+
  geom_point(fill = "white", size = 3.5, shape = 21)+
  geom_text_repel(data = press_br %>% group_by(press) %>%  top_n(1, n), aes(color = press,
    label= prettyNum(n, 
                                 big.mark = ".")), 
            hjust=-.6, vjust=.4, size = 7, fontface = "bold",
            family = "Roboto Condensed")  + # keep expand
  labs(x="", y="Posts\n",
        caption="<br>**Source**: Globo e Folha<br>") + 
 theme(legend.position = "none") +
  scale_x_date(limits = as.Date(c('2014/1/1', '2022/12/1')),
               date_breaks   = "2 months", 
               labels=date_format("%m\n%y"), 
               expand = c(0,0))+ tema +
  theme(
    plot.title.position = "plot",
    plot.caption = element_textbox_simple(hjust = 0, halign = T),
    plot.subtitle = element_textbox_simple(
      lineheight = 1,
      padding = margin(5.5, 5.5, 5.5, 5.5),
      margin = margin(0, 0, 5.5, 0)
    )) +
  scale_y_continuous(labels = addUnits)



```


## Daily data

```{r ag_day, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=8}


t <- agregado %>% 
  count(dia) %>% 
  na.omit() 

t %>% 
  ggplot(., aes(x = dia, y = n)) +
  geom_line(color= "black", alpha = .2) + 
  geom_ma(color= cor,ma_fun = EMA, n = 7, linetype = 1, size = 1.5) +
  geom_text_repel(data = t %>%  top_n(1, n), 
                  aes(label= paste0(dia, "\n",
                                    prettyNum(n, big.mark = "."))
                                                 ), 
            hjust=-.6, vjust=.4, size = 7, 
            family = "Cambria",
            color = cor) +
  labs(x="", y="Pposts\n",
         caption="<br>**Source**: Twitter, Instagram e Facebook<br>") + 
  theme(legend.position = "none") +
  scale_x_date(limits = as.Date(c('2014/1/1', '2022/12/1')),
               date_breaks   = "2 months", 
               labels=date_format("%m\n%y"), 
               expand = c(0,0))+ tema +
  theme(
    plot.title.position = "plot",
    plot.caption = element_textbox_simple(hjust = 0, halign = T),
    plot.subtitle = element_textbox_simple(
      lineheight = 1,
      padding = margin(5.5, 5.5, 5.5, 5.5),
      margin = margin(0, 0, 5.5, 0)
    )) +
  scale_y_continuous(labels = addUnits)


```


# Comparison {.tabset .tabset-pills}

## Daily

```{r ag_comp, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=14}
t <- agregado %>% 
  count(dia, platform) %>% 
  na.omit() 

t %>% 
    ggplot(., aes(x = dia, y = n, group = platform)) +
  geom_line(aes(color= platform), alpha = .2) + 
  geom_ma(aes(color= platform), ma_fun = EMA, n = 7, linetype = 1, size = 1.5)  +
  labs(x="", y="Posts\n",
          caption="<br>**Source**: Twitter Instagram e Facebook<br>") + 
  facet_wrap(~ platform, nrow = 3, scales = "free") +
  scale_x_date(limits = as.Date(c('2014/1/1', '2022/12/1')),
               date_breaks   = "2 months", 
               labels=date_format("%m\n%y"), 
               expand = c(0,0))+ tema +
  theme(plot.caption = element_textbox_simple(hjust = 0, halign = T),
   ) +
  scale_y_continuous(labels = addUnits)+ 
  theme(legend.position = "bottom") +
  theme(strip.text.x = element_text(size = 11,  color = "grey20"))


```

## Monthly

```{r ag_comp_month, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=14}
t <- agregado %>% 
  count(mes_dia, platform) %>% 
  na.omit() %>%  bind_rows(press_agregado)

t %>% 
    ggplot(aes(x = mes_dia, y = n, group = platform, color= platform)) +
  geom_line(size = 2) + 
  geom_point(fill = "white", size = 2, shape = 21)+ 
  labs(x="", y="Posts\n",
          caption="<br>**Source**: Twitter Instagram e Facebook<br>") + 
  facet_wrap(~ platform, nrow = 4, scales = "free") +
  scale_x_date(limits = as.Date(c('2014/1/1', '2022/12/1')),
               date_breaks   = "2 months", 
               labels=date_format("%m\n%y"), 
               expand = c(0,0))+ tema +
  theme(plot.caption = element_textbox_simple(hjust = 0, halign = T),
   ) +
  scale_y_continuous(labels = addUnits)+ 
  theme(legend.position = "none") +
  theme(strip.text.x = element_text(size = 11,  color = "grey20"))


```


# Correlação {.tabset .tabset-pills}


```{r correl_data, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=14}
t <- agregado %>% 
  count(mes_dia, platform) %>% 
  na.omit() %>%  bind_rows(press_agregado) %>%
  pivot_wider(names_from = platform, values_from = n) %>% 
  na.omit()

rownames(t) <- t$mes_dia
t$mes_dia <- NULL

```

## Correlation

```{r cor, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=14}
df_cor <- cor(t)


kpi <- reactable(
  df_cor,
  highlight = T,
  fullWidth = F,
  defaultColDef = colDef(align = "center", minWidth = 100, 
                         format = colFormat(digits = 4)),
  style = list(fontFamily = "Cambria" 
               )
)


div(kpi, align = "center")


```

## P-value

```{r correl_p, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=14}

# matrix of the p-value of the correlation
p.mat <- cor.mtest(t)


kpi <- reactable(
  p.mat,
  highlight = T,
  fullWidth = F,
  defaultColDef = colDef(align = "center", 
                         format = colFormat(digits = 4),
                         minWidth = 100),
  columns = list(
    Valores = colDef(name = "Valores", 
                          cell = function(value){value <- format(value, big.mark = ".")}
    )
  ),
  style = list(fontFamily = "Cambria")
)


div(kpi, align = "center")


```


## Correlogram - Social media

```{r correl_plot, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=14}

t_sep <- agregado %>% 
  count(mes_dia) %>% 
  add_column(platform = "Social_media") |> 
  na.omit() %>%  bind_rows(press_agregado) %>%
  pivot_wider(names_from = platform, values_from = n) %>% 
  na.omit()

rownames(t_sep) <- t_sep$mes_dia
t_sep$mes_dia <- NULL


tab_corr(t_sep, show.p = T,  p.numeric = T)

```


## Correlogram - Separated

```{r correl_plot_sep, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=14}

#col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
#corrplot(df_cor, method="color", col=col(200),  
#         type="upper", order="hclust", number.cex = 2,
#         cl.cex = 2,
#         addCoef.col = "black", # Add coefficient of correlation
#         tl.col="black", tl.cex =  2, 
#         #tl.srt=45, #Text label color and rotation
         # Combine with significance
#         p.mat = p.mat, 
#         sig.level = -1, 
#         insig = "p-value", 
         # hide correlation coefficient on the principal diagonal
#         diag=FALSE  
#         ) 


tab_corr(t, show.p = T,  p.numeric = T)

```



## Scatterplot

```{r scatterplot, align = "center", message = F, error=F, echo = F, warning = F, fig.width=10, fig.height=8}

g <- ggpairs(t, 
             lower  = list(continuous=wrap("smooth", colour="skyblue", fill = "skyblue")) ,
             upper  = list(continuous = "blank"),
             diag  = list(continuous = "blankDiag") 
)+ tema  +
  theme(strip.text.x = element_text(size = 11,  color = "grey20")) +
  theme(strip.text.y = element_text(size = 11, color = "grey20"))  +
  scale_y_continuous(labels = addUnits) +
  scale_x_continuous(labels = addUnits)

gpairs_lower(g)




```



## Scatterplot - Social Media

```{r scatterplot_sep, align = "center", message = F, error=F, echo = F, warning = F, fig.width=10, fig.height=8}

g <- ggpairs(t_sep, 
             lower  = list(continuous=wrap("smooth", colour="skyblue", fill = "skyblue")) ,
             upper  = list(continuous = "blank"),
             diag  = list(continuous = "blankDiag") 
)+ tema  +
  theme(strip.text.x = element_text(size = 11,  color = "grey20")) +
  theme(strip.text.y = element_text(size = 11, color = "grey20"))  +
  scale_y_continuous(labels = addUnits) +
  scale_x_continuous(labels = addUnits)

gpairs_lower(g)




```
